package adhoc.tel.fer.hr;

import java.util.Map;
import java.util.Map.Entry;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * Handles list of ignored friends.
 */
public class IgnoreList extends ListActivity  {

	/** Path to structure with stored ignored friends.*/
	private static final String PREFS_NAME_IGNORED = "ignored.pref";
	
	private SharedPreferences ignoredPref;
	private SQLiteDatabase database;
	
	/** IDs of ignored friends.*/
	private String[] ignoredID;
	
	/** Names of ignored friends.*/
	private String[] ignoredNames;
	
	/** Map with ignored friend's names.*/
	private Map<String, ?> ignoredMap;
	
	/** Define layouta and list content.*/
	private ArrayAdapter<String> ar;
	
	/** User ID fetched from Facebook.*/
	private String id;
	
	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
    	setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);    
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ignored_background);
		
		ignoredPref = getSharedPreferences(PREFS_NAME_IGNORED, 0);
		
		id = PreferencesHelper.getFacebookID();
		
		// Fill map with content in shared prefs
		ignoredMap = ignoredPref.getAll();
		ignoredID = new String[ignoredMap.size()];
		ignoredNames = new String[ignoredMap.size()];
		
		int i = 0;
		for (Entry<String, ?> entry : ignoredMap.entrySet()){
			ignoredID[i] = entry.getKey();
			ignoredNames[i] = (String) entry.getValue();
			i++;
		} 
		
		// Define layout and list content
		ar = new ArrayAdapter<String>(IgnoreList.this.getBaseContext(), R.layout.new_friend_row,
										R.id.add_label, ignoredNames);
		setListAdapter(ar);
	}
	
	
	
	@Override
	protected void onListItemClick(ListView l, View v, final int position, long id) {
		
		// List click listener!
		
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setMessage("Return " + ignoredNames[position] + " to friends list?")
		       .setCancelable(false)
		       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		        	  
		        	SharedPreferences.Editor editor = ignoredPref.edit();
		       		editor.remove(ignoredID[position]);
		       		// Commit the edits!
		       		editor.commit();
		       		
		       		// Send request to the main server
		       		new Thread(new Runnable() {
						
						@Override
						public void run() {
							Util.makeGetRequest(Config.WEB_SERVICE_URL+"/graph?method=addIgnoredFriend&&fb_id=" + 
									IgnoreList.this.id + "&&fr_fb_id=" + ignoredID[position]);
						}
					}).start();
		           }
		       })
		       .setNegativeButton("No", new DialogInterface.OnClickListener() {
		           public void onClick(DialogInterface dialog, int id) {
		                dialog.cancel();
		           }
		       });
		
		AlertDialog alert = builder.create();
		alert.show();
	}
	
	@Override
	protected void onDestroy() {
		database.close();
		super.onDestroy();
	}
}
